<template>

  <ele-form
    ref="eleForm"
    v-model="resetPassword"
    :form-desc="formDesc"
    :request-fn="handleSubmit"
    :rules="rules"
    label-position="right"
    :is-loading="isLoading"
  />

</template>

<script>
import api from '@/api/sys-user'
export default {
  data() {
    return {
      isLoading: false,
      resetPassword: {
        password: '',
        newPassword: '',
        confirmPassword: ''
      },
      formDesc: {
        password: {
          type: 'password',
          label: '原密码'
        },
        newPassword: {
          type: 'password',
          label: '新密码'
        },
        confirmPassword: {
          type: 'password',
          label: '确认密码'
        }
      },
      rules: {
        password: [
          { required: true, message: '原密码不能为空' }
        ],
        newPassword: [{ required: true, message: '新密码不能为空', trigger: 'blur' }],

        confirmPassword: [{ required: true, message: '确认密码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleSubmit(data) {
      if (this.resetPassword.newPassword !== this.resetPassword.confirmPassword) {
        this.$XModal.message({
          message: '新密码和确认密码不一致',
          status: 'error'
        })
        return
      }
      this.isLoading = true
      api.changePassword(data).then(() => {
        this.isLoading = false
        this.$XModal.message({
          message: '密码修改成功',
          status: 'success'
        })

        setTimeout(() => { this.logout() }, 2000)
      }, () => { this.isLoading = false })
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    }
  }
}
</script>
